import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
let app_user_token = 'app_user_token';

function getUserInfo(state) {
	if (state.userInfo) return state.userInfo;
	let userInfo = uni.getStorageSync(app_user_token);
	if (userInfo) {
		state.userInfo = JSON.parse(userInfo);
	}
	return state.userInfo;
}
const store = new Vuex.Store({
	state: {
		IP: uni.getStorageSync('IP') || '',
		userName: '',
		test: '',
		permission: [],
		userInfo: null,
		salesContractDataStore: {}, //省去页面传参
		pageIDStore: '', //省去页面传参
		pushBoxText: uni.getStorageSync('pushBoxText') || '', //透传消息展示内容
		operations_recycleList: [], //运维回收单新建子表明细
		pushDetailArr: [] //明细表增加数组类型的数据
	},
	mutations: {
		// 更改运维回收单新建子表明细
		setOperations_recycleList(state, value) {
			// console.log('val', value.value[value.key]);
			// console.log('state.operations_recycleList', state.operations_recycleList);
			const filter = state.operations_recycleList.filter((item) => item[value.key] == value.value[value.key]);
			if (filter.length) {
				uni.showToast({
					title: '已包含该单据',
					duration: 2000,
					icon: 'error'
				});
			} else {
				state.operations_recycleList.push(value.value);
				uni.showToast({
					title: '添加成功',
					duration: 500,
					icon: 'success'
				});
				setTimeout(()=>{
					uni.navigateBack();
				},2000)
			}
		},
		setOperations_recycleList2(state, value) {
			// console.log('state.operations_recycleList', state.operations_recycleList);
			// console.log('value', value);
			const filter = state.operations_recycleList.filter((item) => item.Fittings_deliveryList == value.Fittings_deliveryList);
			if (filter.length) {
				uni.showToast({
					title: '已包含该单据',
					duration: 2000,
					icon: 'error'
				});
			} else {
				state.operations_recycleList.push(value);
				// uni.navigateBack();
			}
		},
		//push 明细表数组
		setPushDetailArr(state, value) {
			state.pushDetailArr = [];
			state.pushDetailArr = JSON.parse(JSON.stringify(value));
			// const filter = state.operations_recycleList.filter((item) => item.Fittings_deliveryList == value.Fittings_deliveryList);
			// if (filter.length) {
			// 	uni.showToast({
			// 		title: '已包含该单据',
			// 		duration: 2000,
			// 		icon: 'error'
			// 	});
			// } else {
			// 	state.operations_recycleList.push(value);
			// 	// uni.navigateBack();
			// }
		},
		// 删除运维回收单新建子表明细
		deleteOperations_recycleList(state, value) {
			state.operations_recycleList = value;
		},
		// 清空运维回收单新建子表明细
		clearOperations_recycleList(state) {
			state.operations_recycleList = [];
			// console.log('清空后的运维回收单明细', state.operations_recycleList);
		},
		setIP(state, value) {
			state.IP = value;
			uni.setStorage({
				key: 'IP',
				data: value,
				success() {
					console.log('success');
				}
			});
		},
		setUserName(state, value) {
			state.userName = value;
			uni.setStorage({
				key: 'userName',
				data: value,
				success() {
					console.log('success');
				}
			});
		},
		//省去页面传参
		setSalesContractDataStore(state, value) {
			// console.log('store--------value', value);
			state.salesContractDataStore = value;
		},
		setPageIDStore(state, value) {
			// console.log('store--------value2', value);
			state.pageIDStore = value;
		},
		setPushBoxText(state, value) {
			state.pushBoxText = value;
			uni.setStorage({
				key: 'pushBoxText',
				data: value
			});
		},
		removePushBoxText(state, value) {
			state.pushBoxText = '';
			uni.removeStorage({
				key: 'pushBoxText',
				success: function (res) {
					console.log('removePushBoxText');
				}
			});
		},
		setCountInc(state, value) {
			state.test = value;
		},
		setPermission(state, data) {
			//调用方式 this.$store.commit('setPermission', data)
			if (!data || typeof data != 'object') return;
			if (data instanceof Array) {
				state.permission.push(...data);
			} else {
				state.permission = data;
			}
		},
		setUserInfo(state, data) {
			state.userInfo = data;
			uni.setStorageSync(app_user_token, JSON.stringify(data));
		},
		clearUserInfo(state) {
			state.permission = [];
			state.userInfo = null;
			uni.clearStorage(app_user_token);
		}
	},
	actions: {
		setPermission(context, data) {
			context.commit('setPermission', data); //调用方式 store.dispatch('push')
		}
	},
	getters: {
		test(state, getters) {
			return state.test;
		},
		getPermission: (state) => (path) => {
			//调用方式 store.getters.getPermission('sys_User')
			if (!path) return state.permission;
			return state.permission.find((x) => x.path == path);
		},
		getMenu: (state) => () => {
			return state.permission || [];
		},
		getUserInfo: (state) => () => {
			getUserInfo(state);
			return state.userInfo;
		},
		getUserName: (state) => () => {
			getUserInfo(state);
			if (state.userInfo) {
				return state.userInfo.userName;
			}
			return '未获取到登陆信息';
		},
		getToken: (state) => () => {
			getUserInfo(state);
			if (state.userInfo) {
				return 'Bearer ' + state.userInfo.token;
			}
			return '';
		},
		isLogin: (state) => () => {
			if (getUserInfo(state)) {
				return true;
			}
			return false;
		}
	}
});
export default store;
